package com.betty.cscams.controller;

import com.betty.cscams.common.http.HttpResult;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.entity.BusTerm;
import com.betty.cscams.entity.PsyScore;
import com.betty.cscams.service.BusTermService;
import com.betty.cscams.service.PsyScoreService;
import com.betty.cscams.util.SecurityUtils;
import com.betty.cscams.vo.ScoreQueryBean;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ---------------------------
 *  心理测评成绩Controller(PsyScoreController)         
 * ---------------------------
 */
@RestController
@RequestMapping("psyScore")
public class PsyScoreController {

    @Autowired
    private PsyScoreService psyScoreService;
    
    @Autowired
    private BusTermService busTermService;

    /**
     * 保存成绩
     * @param record 成绩记录
     * @return 操作结果
     */    
    @PostMapping(value="/save")
    public HttpResult save(@RequestBody PsyScore record) {
        // 检查学期是否有效
        BusTerm busTerm = busTermService.findById(record.getTermId());
        if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
            return HttpResult.error("当前学期未开始或已结束");
        }
        
        return HttpResult.ok(psyScoreService.save(record));
    }

    /**
     * 删除成绩
     * @param records 成绩列表
     * @return 操作结果
     */
    @PostMapping(value="/delete")
    public HttpResult delete(@RequestBody List<PsyScore> records) {
        return HttpResult.ok(psyScoreService.delete(records));
    }

    /**
     * 分页查询成绩
     * @param pageRequest 分页请求
     * @return 分页结果
     */    
    @PostMapping(value="/findPage")
    public HttpResult findPage(@RequestBody PageRequest pageRequest) {
        return HttpResult.ok(psyScoreService.findPage(pageRequest));
    }
    
    /**
     * 根据ID查询成绩
     * @param id 成绩ID
     * @return 成绩信息
     */ 	
    @GetMapping(value="/findById")
    public HttpResult findById(@RequestParam Long id) {
        return HttpResult.ok(psyScoreService.findById(id));
    }
    
    /**
     * 查询当前用户的成绩
     * @param termId 学期ID
     * @return 成绩信息
     */
    @GetMapping(value="/findByCurrentUser")
    public HttpResult findByCurrentUser(@RequestParam Long termId) {
        String studentNum = SecurityUtils.getUsername();
        return HttpResult.ok(psyScoreService.findByStudentAndTerm(studentNum, termId));
    }
    
    /**
     * 计算班级排名
     * @param params 参数对象
     * @return 操作结果
     */
    @PostMapping(value="/calculateRanking")
    public HttpResult calculateRanking(@RequestBody ScoreQueryBean params) {
        // 检查学期是否有效
        BusTerm busTerm = busTermService.findById(params.getTermId());
        if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
            return HttpResult.error("当前学期未开始或已结束");
        }
        
        try {
            psyScoreService.calculateRanking(params.getTermId(), params.getDeptId());
            return HttpResult.ok("排名计算成功");
        } catch (Exception e) {
            return HttpResult.error("排名计算失败: " + e.getMessage());
        }
    }
    
    /**
     * 导出成绩
     * @param params 查询参数
     * @param response HTTP响应
     * @return 操作结果
     */
    @PostMapping(value="/exportScores")
    public void exportScores(@RequestBody ScoreQueryBean params, HttpServletResponse response) {
        psyScoreService.exportScores(params, response);
    }
} 